﻿using FlexFlow.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlexFlow.Runtime
{
    /// <summary>
    /// 表示用户关系管理器
    /// </summary>
    public class UserRelationManager : ManagerBase
    {
        /// <summary>
        /// 获取或设置引擎默认的用户关系管理器
        /// </summary>
        public static UserRelationManager Default { get; set; }
        /// <summary>
        /// 取得指定组织机构下的所有成员
        /// </summary>
        /// <param name="OrgId">
        /// 要取得成员的组织机构ID
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public OperateResult GetRelations(Guid OrgId)
        {
            return OperateResult.SuccessResult(this.Components.UserRelationProvider.GetRelations(OrgId));
        }
        /// <summary>
        /// 向指定的组织机构中加入指定的成员
        /// </summary>
        /// <param name="orgId">
        /// 要加入成员的组织机构ID
        /// </param>
        /// <param name="memberId">
        /// 要加入的成员ID
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public OperateResult AddMember(Guid orgId, Guid memberId)
        {

            if (this.Components.UserRelationProvider.Exist(orgId, memberId))
            {
                return new OperateResult()
                {
                    ResultCode = 0x030101,
                    Message = "指定关系已经存在！"
                };
            }

            UserRelation relation = this.Components.UserRelationProvider.Create();
            relation.Member = memberId;
            relation.Organization = orgId;

            if (this.Components.UserRelationProvider.Add(relation))
            {
                return OperateResult.SuccessResult(relation.Identity);
            }
            else
            {
                return new OperateResult()
                {
                    ResultCode = 0x030102,
                    Message = "加入关系失败！"
                };
            }
        }

        /// <summary>
        /// 创建并添加组织机构成员
        /// </summary>
        /// <param name="orgId">
        /// 要添加组织机构成员的组织机构ID
        /// </param>
        /// <param name="name">
        /// 组织机构成员的名称
        /// </param>
        /// <param name="account">
        /// 组织机构成员的登录账号
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public OperateResult CreateAndAddMember(Guid orgId, string name, string account, string password = null)
        {
            if (this.Components.UserProvider.CheckAccountExisted(account))
            {
                return new OperateResult()
                {
                    ResultCode = 0x030201,
                    Message = "指定的账号已存在！"
                };
            }
            Organization org = this.Components.OrganizeProvider.Get(orgId);
            if (org == null)
            {
                return new OperateResult()
                {
                    ResultCode = 0x030202,
                    Message = "指定的组织机构ID不存在！"
                };
            }
            User userEntity = this.Components.UserProvider.Create();
            userEntity.Name = name;
            userEntity.Account = account;
            userEntity.Password = password == null ? UserManager.DefaultPassword : password;
            userEntity.Organization = orgId;
            userEntity.OrganizeName = org.Name;
            if (!this.Components.UserProvider.Add(userEntity))
            {
                return new OperateResult()
                {
                    ResultCode = 0x030203,
                    Message = "用户实体加入失败！"
                };
            }
            OperateResult res = AddMember(orgId, userEntity.Identity);
            if (res.ResultCode != 200)
            {
                //回滚操作
                this.Components.UserProvider.Delete(userEntity);
                //返回失败结果
                return new OperateResult()
                {
                    ResultCode = 0x030204,
                    Message = "用户组织机构关联失败！",
                    Tag = res
                };
            }

            return OperateResult.SuccessResult(res.Tag);
        }
        /// <summary>
        /// 从指定的组织机构中移除指定的成员
        /// </summary>
        /// <param name="orgId">
        /// 要移除成员的组织机构ID
        /// </param>
        /// <param name="memberId">
        /// 要移除的成员ID
        /// </param>
        /// <returns>
        /// 移除结果
        /// </returns>
        public OperateResult RemoveMember(Guid orgId, Guid memberId)
        {
            UserRelation relation = this.Components.UserRelationProvider.Get(orgId, memberId);
            if (relation == null)
            {
                return new OperateResult()
                {
                    ResultCode = 0x030301,
                    Message = "指定关系不存在！"
                };
            }

            if (!this.Components.UserRelationProvider.Delete(relation))
            {
                return new OperateResult()
                {
                    ResultCode = 0x030302,
                    Message = "删除关系失败！"
                };
            }

            return OperateResult.SuccessResult();
        }
        /// <summary>
        /// 设定指定ID的关系关联的组织机构
        /// </summary>
        /// <param name="relId">
        /// 要设置的关系ID
        /// </param>
        /// <param name="OrgId">
        /// 要设置的组织机构ID
        /// </param>
        /// <returns>
        /// 一个表示是否设置成功的值
        /// </returns>
        public OperateResult SetOrg(Guid relId, Guid OrgId)
        {
            if (!this.Components.OrganizeProvider.Exist(OrgId))
            {
                return new OperateResult()
                {
                    ResultCode = 0x030401,
                    Message = "指定的组织机构ID不存在！"
                };
            }

            if (!this.Components.UserRelationProvider.SetOrg(relId, OrgId))
            {
                return new OperateResult()
                {
                    ResultCode = 0x030402,
                    Message = "更新数据失败！请检查relID是否正确！"
                };
            }

            return OperateResult.SuccessResult();
        }
        /// <summary>
        /// 删除具有指定ID的用户关系实体
        /// </summary>
        /// <param name="identity">
        /// 要删除的实体的ID
        /// </param>
        /// <returns>
        /// 一个表示是否删除成功的值
        /// </returns>
        public OperateResult Delete(Guid identity)
        {
            if (!this.Components.UserRelationProvider.Delete(identity))
            {
                return new OperateResult()
                {
                    ResultCode = 0x030501,
                    Message = "删除实体失败！"
                };
            }

            return OperateResult.SuccessResult();
        }
        /// <summary>
        /// 向指定的组织机构中加入指定的成员
        /// </summary>
        /// <param name="orgId">
        /// 要加入成员的组织机构ID
        /// </param>
        /// <param name="userAccount">
        /// 要加入的成员账号
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public OperateResult AddMember(Guid orgId, string userAccount)
        {
            User user = this.Components.UserProvider.Get(userAccount);
            if (user == null)
            {
                return new OperateResult()
                {
                    ResultCode = 0x030601,
                    Message = "指定用户账户不存在！"
                };
            }

            return AddMember(orgId, user.Identity);
        }
        /// <summary>
        /// 初始化用户关系管理器的默认配置
        /// </summary>
        static UserRelationManager()
        {
            Default = new UserRelationManager();
        }
    }
}
